package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.base.dao.BaseDao;
import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.system.web.api.entity.FeedsRecordActiveMqEntity;
import com.frogs.ucenter.dao.FamilyFeedsRelationDao;
import com.frogs.ucenter.dao.UserFeedsDao;
import com.frogs.ucenter.entity.FamilyFeedsRelation;
import com.frogs.ucenter.entity.UserFeeds;
import com.frogs.ucenter.v1.service.UserFeedsService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@Service(ApiConstants.VERSION + "userFeedsService")
public class UserFeedsServiceImpl extends BaseServiceImpl<UserFeeds> implements UserFeedsService {
	
	private static final Logger log = LoggerFactory.getLogger(UserFeedsServiceImpl.class);
	@Resource
	private UserFeedsDao userFeedsDao;
	@Resource
	private FamilyFeedsRelationDao familyFeedsRelationDao;
	
	@Override
	public void insertFeedsRecordList(FeedsRecordActiveMqEntity activeMqEntity) {
		
		try {
			log.info("insertFeedsRecordList方法的参数数据："+JSON.toJSONString(activeMqEntity));  
			UserFeeds userFeeds = new UserFeeds();
			userFeeds.setActionRecord(activeMqEntity.getActionRecord());
			userFeeds.setCreateTime(new Date());
			userFeeds.setFollowedUserId(activeMqEntity.getFollowedUserId());
			userFeeds.setMessage(activeMqEntity.getMessage());
			userFeeds.setType(activeMqEntity.getType());
			userFeeds.setFollowUserId(activeMqEntity.getFollowUserId());
			userFeedsDao.insert(userFeeds);
			//插入家庭动态表数据
			insertFamilyFeedsRelation(userFeeds.getId(),activeMqEntity.getFamilyId());
		} catch (Exception e) {
			log.error("func[{}] params[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						JSONObject.toJSONString(activeMqEntity) ,Arrays.deepToString(e.getStackTrace()) });
		}
	}

	private void insertFamilyFeedsRelation(int feedsId,int familyId) {
		try {
			FamilyFeedsRelation familyFeedsRelation = new FamilyFeedsRelation();
			familyFeedsRelation.setFamilyId(familyId);
			familyFeedsRelation.setFeedsId(feedsId);
			familyFeedsRelationDao.insert(familyFeedsRelation);
		} catch (BeansException e) {
			log.error("func[{}] params[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						"动态记录id："+feedsId+"， 家庭id号："+familyId ,Arrays.deepToString(e.getStackTrace()) });
		} 
	}
}
